const dStoreModel = require('./../models/dStoreModel');
var sd = require('silly-datetime');
const uuidV1 = require('uuid/v1');

const dStoreService = {
    async getAllStoreList(){
        return await dStoreModel.getAllStoreList();
    },

    async getBannerList(){
        return await dStoreModel.getBannerList();
    },

    async getTodayList(){
        return await dStoreModel.getTodayList();
    },

    async getUserId(phone){
        let result = await dStoreModel.getUserByPhone(phone);
        if(result.length > 0) {
            return result[0]['user_id']
        }else {
            let userId = uuidV1();

            let result = await dStoreModel.createUser({
                'phone': phone,
                'create_time':sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss'),
                'user_id': uuidV1()
            });

            if(result.affectedRows > 0) {
                return userId
            }
        }
    },

    async getRecentDateList(){
        return await dStoreModel.getRecentDateList();
    },

    async getDateItem(date){
        return await dStoreModel.getDateItem(date);
    },

    async getItemByKeyword(keyword){
        return await dStoreModel.getItemByKeyword(keyword);
    },

};

module.exports = dStoreService;